package net.suplementos.web.action;

import net.suplementos.negocio.ControleDelegate;
import net.suplementos.pojo.Fornecedor;
import net.suplementos.pojo.Marca;
import net.suplementos.servico.ControleServico;
import net.suplementos.web.LocalConstantes;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ActionSupport;

@Namespace("/associarFornecedorMarca")
@InterceptorRefs({
	@InterceptorRef(value = "store", params = {"operationMode", "AUTOMATIC"}),
	@InterceptorRef("defaultStack")
})
public class AssociarFornecedorMarcaAction extends ActionSupport {
	
	private Fornecedor fornecedor;
	private Marca marca;
	private ControleServico controleServico = ControleDelegate.getInstance();

	@Action(value = "abrirAssociarDesassociar", results = {
		@Result(name = SUCCESS, location = LocalConstantes.LOCAL_JSP_CONTROLE_ASSOCIAR_FORNECEDOR_MARCA + "/associar.jsp") 
	})
    public String abrirAssociarDesassociar() throws Exception {
        return SUCCESS;
    }

	@Action(value = "consultarMarca", results = {
		@Result(name = SUCCESS, location = LocalConstantes.LOCAL_JSP_CONTROLE_ASSOCIAR_FORNECEDOR_MARCA + "/associar.jsp") 
	})
	public String consultarMarca() throws Exception {
		return SUCCESS;
	}

	@Action(value = "associar", results = {
		@Result(name = SUCCESS, params = {"actionName", "consultarMarca"}, type = "redirectAction") 
	})
	public String associar() throws Exception {
		addActionMessage(getText("associar.fornecedor.mensagem.sucesso", new String[]{"Marca"}));
		controleServico.associarFornecedorMarca(getFornecedor(), getMarca());
		return SUCCESS;
	}

	@Action(value = "desassociar", results = {
		@Result(name = SUCCESS, params = {"actionName", "consultarMarca"}, type = "redirectAction") 
	})
	public String desassociar() throws Exception {
		addActionMessage(getText("desassociar.fornecedor.mensagem.sucesso", new String[]{"Marca"}));
		controleServico.desassociarFornecedorMarca(getFornecedor(), getMarca());
		return SUCCESS;
	}

	public Fornecedor getFornecedor() {
		return fornecedor;
	}

	public void setFornecedor(Fornecedor fornecedor) {
		this.fornecedor = fornecedor;
	}

	public Marca getMarca() {
		return marca;
	}

	public void setMarca(Marca marca) {
		this.marca = marca;
	}

	public ControleServico getControleServico() {
		return controleServico;
	}

	public void setControleServico(ControleServico controleServico) {
		this.controleServico = controleServico;
	}
}
